home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8787 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  82 lines

  1. Path: dragon.solect.com!annex
  2. From: gallantm@kanservu.ca (Matt Gallant)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What is referencing good for?
  5. Date: Mon, 26 Feb 96 06:47:21 GMT
  6. Organization: KanServU Bureau  Aylmer, Ontario Canada
  7. Message-ID: <4grl0b$b2o@dragon.solect.com>
  8. References: <4goojd$a9g@wintermute.ecs.fullerton.edu>
  9. NNTP-Posting-Host: ts1p01.kanservu.ca
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=US-ASCII
  12. X-Newsreader: News Xpress 2.0 Beta #0
  13.  
  14. In article <4goojd$a9g@wintermute.ecs.fullerton.edu>, grosin@titan.fullerton.edu (Gil Rosin) wrote:
  15. >I don't quite understand what reference functions are good for. Again, I can
  16. >do the same stuff with pointers.
  17. >
  18. >Can someone give me a REAL world example of where I would use referencing
  19. >perhaps somewhere that I can not use pointers? 
  20. >
  21.  
  22. Well, you "hit the nail on the head" when you said that you can do everything 
  23. with pointers that you can pretty much do with references and that references 
  24. just seem to give cleaner code.
  25.  
  26. With references, you let the compiler handle all of the pointer manipulation 
  27. and dereferencing.  This was added to C++ because in C (which does not have 
  28. references), using pointers was always the issue that seemed to cause almost 
  29. EVERYONE problems from time to time.  Programmers knew that pointers were more 
  30. efficient, but they often made coding errors the first time around.  
  31. References give you the efficiency automatically, without the need to remember 
  32. to derefernce your pointers and such.
  33.  
  34. References can also be used to 'alias' a variable.  I've been using them in a 
  35. program that I'm writing that uses complex unions and structures.   If I need 
  36. to access a member of a union repeatedly, I make a reference to it and use the 
  37. reference instead of the full name.  Here's an example (it's "meaning" is 
  38. totally BOGUS, it's just a demo)...
  39.  
  40. union {
  41.         char str[4];
  42.         DWORD dw;
  43.         struct {
  44.                 char byte;
  45.                 short index;
  46.                 char c;
  47.         } myStruct;
  48. } myUnion;
  49.  
  50. Now, let's say that I need to access the 'index' member.  Without pointers or 
  51. references, I'd have to type 'myUnion.myStruct.index' every time I need to 
  52. read or write the member.
  53.  
  54. WITH POINTERS----------------------------------
  55. short *theShort = &myUnion.myStruct.index;      // create a pointer to short
  56. .
  57. .
  58. .
  59. //To access it...
  60. short count = *theShort;        // Read it
  61. *theShort = 4;                          // Write it
  62.  
  63. WITH REFERENCES------------------------------
  64. short &theShort = myUnion.myStruct.index;       // 'theShort' becomes an alias
  65. .
  66. .
  67. .
  68. // To access it...
  69. short count = theShort;
  70. theShort = 4;
  71.  
  72. As you can see, the reference version seems more "natural".
  73.  
  74. In short, whether you use one or the other, it's just a matter of personal 
  75. preference.  What's important is that C++ gives you the choice!
  76.  
  77.       
  78.  
  79. --------------------------------------
  80. Matt Gallant
  81. Aylmer, Ontario Canada
  82.